package com.jsh.mypor.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jsh.mypor.pojo.Admin;
import com.jsh.mypor.service.AdminService;
import com.jsh.mypor.util.MD5;
import com.jsh.mypor.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.List;

@RestController
@RequestMapping("/sms/adminController")
public class AdminController {
    @Autowired
    private AdminService adminService;
    //getAllAdmin/1/3
    @GetMapping("/getAllAdmin/{pageNo}/{pageSize}")
    public Result getAllAdmin(
            @PathVariable Integer pageNo,
            @PathVariable Integer pageSize,
            String adminName
    ){
        Page<Admin> pageParam =new Page<Admin>(pageNo,pageSize);
        IPage<Admin> iPage=adminService.getAdminByOpr(pageParam,adminName);
    return Result.ok(iPage);
    }
    @PostMapping("/saveOrUpdateAdmin")
    public Result savaOrUpdateAdmin(
            @RequestBody Admin admin
    ){
        Integer id = admin.getId();
        if (id==null||0==id){
            admin.setPassword(MD5.encrypt(admin.getPassword()));

        }
        adminService.saveOrUpdate(admin);
        return Result.ok();

    }
    @DeleteMapping("/deleteAdmin")
    public Result deleteAdmin(
            @RequestBody List<Integer> ids
    ){
        adminService.removeByIds(ids);
        return Result.ok();
    }
}
